package rickyGame.server.effect
{
	import flash.geom.Point;
	
	import org.hamcrest.object.nullValue;
	
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.utils.TimeServer;
	
	import ricky.game.Actor2D;
	import rickyGame.game.Fighter;
	import rickyGame.game.Player;
	import rickyGame.game.action.AnimaAct;
	import rickyGame.game.action.DeathAct;
	import rickyGame.game.action.HurtAct;
	import rickyGame.game.action.MsgAct;
	import rickyGame.game.action.ai.AutoAttackTarget;
	import rickyGame.game.action.state.InstalChangeGroupAct;
	import rickyGame.game.action.state.InstalSpeedSateAct;
	import rickyGame.game.action.state.InstallFightSateAct;
	import rickyGame.game.action.state.InstallGraphicAct;
	import rickyGame.game.action.state.InstallModel;
	import rickyGame.game.action.state.InstallSkillStateAct;
	import rickyGame.game.graphic.PlayerGraphic;
	import rickyGame.game.property.FightPro;
	import rickyGame.net.MsgType;
	import rickyGame.net.WorkQueue;
	import rickyGame.server.ServerWorld;
	import rickyGame.server.state.CreateArmy;
	import rickyGame.server.state.LongDecisionAi;

	/**
	 * 服务端效果运算辅助类
	 */
	public class EffectHelper
	{
		public function EffectHelper()
		{
			
		}
		
		/**
		 * 改变目标阵营
		 */
		public static function changeGroup(oper:Fighter,tar:Fighter,par:Array):Act2D
		{
			var continueTime:Number;
			continueTime=Number(par[0]);
			var endTime:Number=TimeServer.now()+ continueTime;
			var i:InstalChangeGroupAct=new InstalChangeGroupAct(tar,endTime);
			return i;
		}
		/**
		 * 创建军队
		 */
		public static function createArmy(oper:Fighter,tar:Fighter,par:Array):Act2D
		{
			var pars:Array=[];
			var l:int=par.length;///3;
			var unit:String,num:int,level:int;
			for(var i:int=0;i<l;i=i+3)
			{
				unit=par[i];
				num=par[i+1];
				level=par[i+2];
				pars.push([unit,num,level]);
			}
//			var upar:Array=par
			var ca:CreateArmy=new CreateArmy(oper,pars,null);
//			return ca;
			ca.begin();
			
			var army:Array=ca.army;

			for each(var ac:Actor2D in army)
			{
			  ServerWorld.inst.addObj(ac);
			  if(ac is Fighter)
			  	LongDecisionAi.inst.enterTheArmy(Fighter(ac));
			}
			 
		  if(army.length>0)
		 	 ServerWorld.inst.broadcastAround(ServerWorld.inst.me);
			return null;
		}
		
		/**
		 * 
		 * 让目标变大
		 */
		public static function installGraphicAct(oper:Fighter,tar:Fighter,par:Array):Act2D
		{
			var value:Number,continueTime:Number;
			var index:int=int(par[0]);//作用目标索引
			value=Number(par[1]);
			continueTime=Number(par[2]);
			var endTime:Number=TimeServer.now()+ continueTime;
			var a:Fighter;
			if(index==0)
			{
				a=oper;
			}else
				a=tar;
			
			var ins:InstallGraphicAct=new InstallGraphicAct(a,value,endTime);
			return ins;
		}
		
		/**
		 * 给目标加血加蓝
		 */
		public static function plusFightProperty(oper:Fighter,tar:Fighter,par:Array):Act2D
		{
			var pars:Array=par;
			
			var index:int=int(pars[0]);//作用目标索引
			var property:String=pars[1];
			var value:Number=Number(pars[2]);
			
			var target:Fighter;
			var fight:FightPro;//=oper.fightPro;
//			var tarFight:FightPro=tar.fightPro;
			
			if(index==0) //自己
			{
				fight=oper.fightPro;
				target=oper;
			}
			else //目标
			{
				target=tar;
				fight=tar.fightPro;//[property].agileValue=tarFight[property].agileValue+value;
			}
			
				fight[property].agileValue=fight[property].agileValue+value;

				if(target && target.graphic is PlayerGraphic)
					PlayerGraphic(target.graphic).showBloodBar();
				
			return null;
		}
		
		/**
		 * 魔法伤害
		 * par:* 伤害值
		 */
		public static function manaInjure(oper:Fighter,tar:Fighter,par:*):Act2D
		{
		
			var fight:FightPro=oper.fightPro;
			var tarFight:FightPro=tar.fightPro;

			var h:Number=Number(par);
			var magicShun:int=tarFight.magicShun.value;
			if(magicShun>30)
				magicShun=25;
			var r:int=100*Math.random();
			var def:int=tarFight.magicDefense.value;
			if(r<magicShun)
			{
				h=10;
			}else
			{
				h=fight.magicAttack.value+h;
				h=h-def;
				if(h<=0)
					h=fight.magicAttack.value*0.1;
				else
					h=-h;
			}
			
			var mc:Act2D=new HurtAct(tar,h);
			return mc;
		}
		
		/**
		 * 普通攻击jsua
		 */
		public static function normanInjure(oper:Fighter,tar:Fighter,par:*):Act2D
		{
			
			var h:Number=Number(par);
			var shun:int=tar.fightPro.physicalShun.value;
			var r:int=100*Math.random();
			var def:int=tar.fightPro.physicalDefense.value;
			if(r<shun)
			{
				h=0;
			}else
			{
				var a:Number=oper.fightPro.physicalAttack.value;
				h=a+h;
				h=h-def;
				if(h<=0)
					h=0;
				else
					h=-h;
			}
			
			var n:int=Math.random()*100;
			
			
			tar.ai.hurtMe=oper;
			var mc:Act2D=new HurtAct(tar,h);
			if(n<20)
				mc.addEffectNext(new AutoAttackTarget(tar,oper));//20机会F反击
			return mc;
		}
		
		/**
		 * 安装状态魔法效果
		 */
		 public static function installState(oper:Fighter,tar:Fighter,par:Array):Act2D
		 {
			 var statModel:String=par[0];
			 var ctime:int=int(par[1]);
			 var position:int=int(par[2]);
			var endTime:Number=TimeServer.now()+ ctime;
			 var ins:InstallSkillStateAct=new InstallSkillStateAct(tar,statModel,endTime,position);
			 return ins;
		 }
		 
		 /**
		  * 安装战斗属性状态
		  * 参数par=[fightPro,fightvalue,ctime]
		  */
		 public static function installFightSate(oper:Fighter,tar:Fighter,par:Array):Act2D
		 {
			 var fightPro:String=par[0];
			 var fightvalue:Number=Number(par[1]);
			 var ctime:int=int(par[2]);
			 
			 var endTime:Number=TimeServer.now()+ ctime;
			 var ins:InstallFightSateAct=new InstallFightSateAct(tar,fightPro,fightvalue,endTime);
			 return ins;
		 }
		 
		 /**
		 * 安装移动属性状态
		 * 参数par=[speed,endTime];
		 */
		 public static function installSpeedSate(oper:Fighter,tar:Fighter,par:Array):Act2D
		 {
			 var  speed:Number=Number(par[0]);
			 var endTime:Number=Number(par[1]);
			 endTime= TimeServer.now()+ endTime;
			 var i:InstalSpeedSateAct=new InstalSpeedSateAct(tar,speed,endTime);
			 return i;
		 }
		 
		 /**
		  * 安装改变模型属性状态
		  * 参数par=[model,endTime];
		  */
		 public static function installModel(oper:Fighter,tar:Fighter,par:Array):Act2D
		 {
			 var model:String=par[0];
			 var endTime:Number=Number(par[1]);
			 endTime= TimeServer.now()+ endTime;

			 var i:InstallModel=new InstallModel(tar,model,endTime);
			 return i;
		 }
		
		 /**
		 * 安装死亡状态删除对象
		 */
		 public static function installDeathSate(oper:Fighter,tar:Actor,par:Array):Act2D
		 {
//			 var model:String=par[0];
			 var endTime:Number=Number(par[0]);
//			 endTime= TimeServer.now()+ endTime;
			 
			 var i:DeathAct=new DeathAct(tar,endTime);
			 return i;
		 }
		 
//		 /**
//		 * 创建箭支
//		 */
//		 public static function createArrow(oper:Fighter,tar:Actor,par:Array):Act
//		 {
//			 
//		 }
		 
		 /**
		 * 删除目标对象
		 */
		 public static function deleteTarget(oper:Fighter,tar:Actor,par:Array):Act2D
		 {
//			 WorkQueue.instance.pushRecive(MsgType.CLIENT_DELETE_ACTOR,tar);
			 return null;
		 }
			 
		public static function analyzeEffect(fun:String):Array
		{

			var fi:int=fun.indexOf("(");
			var ei:int=fun.lastIndexOf(")");
			
			var fName:String=fun.substr(0,fi);
			var par:String=fun.substring(fi+1,ei);
			var args:Array=par.toString().split(",");
			var ret:Array=[];
			var item:String;
			var arg:String;
			//check
			for(var i:int=0;i<args.length;i++)
			{
				item=args[i];
				if(item.indexOf("(")>=0 && item.indexOf(")")>=0)
				{
					ret.push(item);
				}
				else if(item.indexOf("(")>=0)//只有开始标签
				{
					arg=item;//开始标签
				}
				else if(item.indexOf("(") ==-1 && item.indexOf(")")==-1 && arg)//中间参数
				{
					arg+=","+item;
				}
				else if(item.indexOf(")")>=0 && arg )//只有结束标签
				{
					arg+=","+item;
					ret.push(arg);
					arg=null;
				}else if(!arg)
				{
					ret.push(item);
				}
			}
			
			var re:Array=new Array();
			re.push(fName);
			re.push(ret);
			
			return re;
		}
	}
}